package cn.itcast.shop.product;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import cn.itcast.shop.category.Category;
import cn.itcast.shop.category.CategoryService;
import cn.itcast.shop.categorysecond.CategorySecond;
import cn.itcast.shop.categorysecond.CategorySecondService;
import cn.itcast.shop.utils.PageBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends ActionSupport implements ModelDriven<Product>{
	
	Product product =new Product();
	
	@Override
	public Product getModel() {
		// TODO Auto-generated method stub
		return product ;
	}
	private String cp;
	public String getCp() {
		return cp;
	}

	public void setCp(String cp) {
		this.cp = cp;
	}
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	
	
	public String getUploadContentType() {
		return uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Integer pid;
	
	public void setPid(Integer pid) {
		this.pid = pid;
	}

	private CategorySecondService categorySecondService;
	
	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}
	private Integer cid;
	
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	
	public Integer getCid() {
		return cid;
	}

	private Integer csid;
	
	public void setCsid(Integer csid) {
		this.csid = csid;
	}

	public Integer getCsid() {
		return csid;
	}

	private ProductService productService;
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	private Integer page;
	
	public void setPage(Integer page) {
		this.page = page;
	}
	private CategoryService categoryService;

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	private PageBean<Product> pageBean;
	
	

	public PageBean<Product> getPageBean() {
		return pageBean;
	}



	public String findByCid(){
		List<Category> categoryList=categoryService.findAll();
		ActionContext.getContext().getValueStack().set("categoryList", categoryList);
		pageBean=productService.findByPage(cid,page);
		return "findByCidSuccess";
	}

	public String findByPid(){
		product=productService.findByPid(product.getPid());
		List<Category> categoryList=categoryService.findAll();
		ActionContext.getContext().getValueStack().set("categoryList", categoryList);
		return "findByPidSuccess";
	}

	public String findByCsid(){
		List<Category> categoryList=categoryService.findAll();
		ActionContext.getContext().getValueStack().set("categoryList", categoryList);
		pageBean=productService.findByCsid(csid,page);
		return "findByCsidSuccess";
	}
	
	public String adminFindAll(){
		pageBean=productService.findByPages(page);
		return "adminFindAllSuccess";
	}
	
	public String addPage(){
		List<CategorySecond> csList=categorySecondService.findAll();
		ActionContext.getContext().getValueStack().set("csList", csList);
		return "addPageSuccess";
	}
	
	public String save() throws IOException{
		String path=ServletActionContext.getServletContext().getRealPath("/products");
		String realPath=path+"/"+csid+"/"+uploadFileName;
		File deskFile=new File(realPath);
		FileUtils.copyFile(upload, deskFile);
		CategorySecond categorySecond=new CategorySecond();
		categorySecond.setCsid(csid);
		product.setCategorySecond(categorySecond);
		product.setPdate(new Date());
		product.setImage("products/"+csid+"/"+uploadFileName);
		productService.save(product);
		return "saveSuccess";
	}
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String welcome(){
		return "welcome";
	}
	
	public String delete(){
		productService.delelte(product);
		return "deleteSuccess";
	}
	public String edit(){
		List<CategorySecond> csList=categorySecondService.findAll();
		ActionContext.getContext().getValueStack().set("csList", csList);
		
		product=productService.findByPid(product.getPid());
		cp=product.getImage();
		return "editSuccess";
	}
	public String update() throws IOException{
	
		if(uploadFileName!=null){
		String path=ServletActionContext.getServletContext().getRealPath("/products");
		String realPath=path+"/"+csid+"/"+uploadFileName;
		File deskFile=new File(realPath);
		
		File deleteFile=new File(cp);
		
		deleteFile.delete();
		FileUtils.copyFile(upload, deskFile);
		product.setImage("products/"+csid+"/"+uploadFileName);
		}else{product.setImage(cp);}
		
		CategorySecond categorySecond=new CategorySecond();
		categorySecond.setCsid(csid);
		product.setCategorySecond(categorySecond);
		product.setPdate(new Date());
		
		productService.update(product);
		return "updateSuccess";
	}
	
	
}
